/************************************************************************************************************************************************************************************************************************************************
Can Mentoring Alleviate Family Disadvantage in Adolescence? A Field Experi-ment to Improve Labor-Market Prospects
by Sven Resnjanskij, Jens Ruhose, Simon Wiederhold, Ludger Woessmann, and Katharina Wedel

Journal of Political Economy

Replication code for tables and figures in the paper and the appendix
************************************************************************************************************************************************************************************************************************************************/

//  STATA Set-up 
cap log close
clear all
timer clear
version 15.1
set rmsg off, perm
set maxvar 11000

// Select Path & working directory
global path "mydirectory"
			
cd "${path}"

// Folder and paths
foreach folder in data table figure results {
  global `folder' ${path}\\`folder'
}

* Load data
use "${data}\\DataMentoring.dta", clear

// Define macros
gl RandomSample "Pairs_NoTwin_w1==0"
gl FE_pair "i.ID_PairsNUM_w1"
gl FEabsorb_pair "ID_PairsNUM_w1"
gl control  "male_w1 age_w1 migrant_w1 privatteach_w1  parentssupport_w1M parentssupport_w1MD bfi_extra_w1 bfi_neur_w1 bfi_open_w1 bfi_con_w1 bfi_agree_w1M bfi_agree_w1MD"


/************************************************************************************************************************************************************************************************************************************************

MAIN TABLES

************************************************************************************************************************************************************************************************************************************************/

// Table 1: Balancing

preserve
	loc dep_OutcomesSummary 	I_lmprospect_w1 math_pre math_preMD I_patsocial_w1 I_labororient_w1
	loc dep_Outcomes 			I_timedisc_w1 I_socialskills_w1 I_prosoc_w1 I_trust_w1 I_selfeff_w1 wish_appr_w1 ZZknowcareerfut_w1
	loc Vars_bal 				male_w1 age_w1 migrant_w1 books_gr_w1 grade_math_gr_w1 grade_math_gr_w1MD grade_german_gr_w1 grade_german_gr_w1MD grade_english_gr_w1 grade_english_w1MD privatteach_w1 parentssupport_w1 bfi_con_w1 bfi_neur_w1
	loc Vars_control 			bfi_open_w1 bfi_extra_w1 bfi_agree_w1 
	loc Vars_SES		 	    HigherSES 

	loc j=1
	foreach covariates in "" "" {
		loc i=1
		foreach IF_sample in "if Pairs_NoTwin_w1==0" {
			disp as err `i'
			loc name1 "full sample"
			disp as err "`name`i''"
			orth_out `dep_OutcomesSummary' 			using "${results}\\Table1-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') replace bdec(3) title(Main outcomes baseline (summary measures)) 
			orth_out `dep_Outcomes' 				using "${results}\\Table1-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Main outcomes baseline (components))
			noi cap orth_out `Vars_bal' 			using "${results}\\Table1-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Matching variables randomization baseline)
			noi cap orth_out `Vars_control'		 	using "${results}\\Table1-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Further control variables baseline)  
			noi cap orth_out `Vars_SES'		 		using "${results}\\Table1-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Further SES variables baseline) notes(Sample: `name`i''; `covariates') count 
			loc ++i	
		}
		loc ++j
	}
	beep
restore

******

** TESTING DIFFERENCES BY SES (Table 1: Balancing, Column (4))

//Note: also generates the other columns of Table 1, but with less nice formatting than orth_out

preserve

ren I_lmprospect_w1 summary_all_w1
ren I_patsocial_w1 summary_social_w1
ren I_labororient_w1 summary_labor_w1

keep if Pairs_NoTwin_w1==0

	loc dep_OutcomesSummary 	summary_all_w1 math_pre math_preMD summary_social_w1 summary_labor_w1
	loc dep_Outcomes 			I_timedisc_w1 I_socialskills_w1 I_prosoc_w1 I_trust_w1 I_selfeff_w1 wish_appr_w1 ZZknowcareerfut_w1
	loc Vars_bal 				male_w1 age_w1 migrant_w1 books_gr_w1 grade_math_gr_w1 grade_math_gr_w1MD grade_german_gr_w1 grade_german_gr_w1MD grade_english_gr_w1 grade_english_w1MD privatteach_w1 parentssupport_w1 bfi_con_w1 bfi_neur_w1
	loc Vars_control 			bfi_open_w1 bfi_extra_w1 bfi_agree_w1 
	loc Vars_SES		 	    HigherSES 
	
eststo clear
foreach var of varlist `dep_OutcomesSummary' {
	reg `var' treatment_w1 , r
	sum `var' if treatment_w1 == 0
	estadd scalar Control_mean = r(mean)
	sum `var' if treatment_w1 == 1
	estadd scalar Treatment_mean = r(mean)
	lincom treatment_w1 											//test whether there is a treatment difference in baseline variable
	estadd scalar Overallest = r(estimate)
	estadd scalar Overallest_SE = r(se)
	estadd scalar Overallest_p = r(p)
	eststo
	
	reg `var'  i.treatment_w1##i.HigherSES , r
	test 1.treatment_w1 1.HigherSES#1.treatment_w1		//test joint significance of treatment effect for lowest-SES and interaction with higher-SES dummy
	estadd scalar SESest_p = r(p)
	eststo
}

foreach var of varlist `dep_Outcomes' {
	reg `var'  treatment_w1 , r
	sum `var' if treatment_w1 == 0
	estadd scalar Control_mean = r(mean)
	sum `var' if treatment_w1 == 1
	estadd scalar Treatment_mean = r(mean)
	lincom treatment_w1
	estadd scalar Overallest = r(estimate)
	estadd scalar Overallest_SE = r(se)
	estadd scalar Overallest_p = r(p)
	eststo
		
	reg `var'  i.treatment_w1##i.HigherSES , r
	test 1.treatment_w1 1.HigherSES#1.treatment_w1
	estadd scalar SESest_p = r(p)
	eststo
}

foreach var of varlist `Vars_bal' {
	reg `var'  treatment_w1 , r
	sum `var' if treatment_w1 == 0
	estadd scalar Control_mean = r(mean)
	sum `var' if treatment_w1 == 1
	estadd scalar Treatment_mean = r(mean)
	lincom treatment_w1
	estadd scalar Overallest = r(estimate)
	estadd scalar Overallest_SE = r(se)
	estadd scalar Overallest_p = r(p)
	eststo
		
	reg `var'  i.treatment_w1##i.HigherSES , r
	test 1.treatment_w1 1.HigherSES#1.treatment_w1
	estadd scalar SESest_p = r(p)
	eststo
}

foreach var of varlist `Vars_control' {
	reg `var'  treatment_w1 , r
	sum `var' if treatment_w1 == 0
	estadd scalar Control_mean = r(mean)
	sum `var' if treatment_w1 == 1
	estadd scalar Treatment_mean = r(mean)
	lincom treatment_w1
	estadd scalar Overallest = r(estimate)
	estadd scalar Overallest_SE = r(se)
	estadd scalar Overallest_p = r(p)
	eststo
		
	reg `var'  i.treatment_w1##i.HigherSES , r
	test 1.treatment_w1 1.HigherSES#1.treatment_w1
	estadd scalar SESest_p = r(p)
	eststo
}

foreach var of varlist `Vars_SES' {
	reg `var'  treatment_w1 , r
	sum `var' if treatment_w1 == 0
	estadd scalar Control_mean = r(mean)
	sum `var' if treatment_w1 == 1
	estadd scalar Treatment_mean = r(mean)
	lincom treatment_w1
	estadd scalar Overallest = r(estimate)
	estadd scalar Overallest_SE = r(se)
	estadd scalar Overallest_p = r(p)
	eststo
	
	reg `var'  i.treatment_w1##i.HigherSES , r
	test 1.treatment_w1 1.HigherSES#1.treatment_w1
	estadd scalar SESest_p = r(p)
	eststo	
	
	ereturn list
}
#delimit;
cd "${results}";
esttab using Table1_DifferecebySES.csv,  
replace star(* 0.10 ** 0.05 *** 0.01) 
drop(*) 
stats(Control_mean Treatment_mean Overallest_p SESest_p N, fmt(2 2 3 3 0) labels("Control mean" "Treatment mean" "p-value" "Treatment difference SES p-value" "Observations" )) 
cells(b(star fmt(3)) se(par fmt(3)) p(par([ ]) fmt(3))) 
order(*treatment_w1)
depvars label nobase scsv
title("Balancing table") 
;
#delimit cr

restore
 
 
// Table 2: Effect of the Mentoring Program on Index of Labor-Market Prospects

* Panel A: Average treatment effects

reg I_lmprospect_w2 i.treatment_w1  if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0) : `e(cmdline)'
	
reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0) : `e(cmdline)'

reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $FE_pair if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0) : `e(cmdline)'

reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0) : `e(cmdline)'

ivreg2 I_lmprospect_w2 (i.participated_w2 = i.treatment_w1) I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r small



* Panel B: Heterogeneous treatment effects by SES
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1  if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0) : `e(cmdline)'

reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $FE_pair if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000)  seed(0)  : `e(cmdline)'

reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample, r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0) : `e(cmdline)'
	
ivreg2 I_lmprospect_w2 (i.HigherSES#ib0.participated_w2 i.participated_w2 = i.HigherSES#ib0.treatment_w1 i.treatment_w1) i.HigherSES I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r small




// Table 3: Effect of the Mentoring Program on Components of the Index of Labor-Market Prospects

* Math grade

reg math_post1 i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

reg math_post1 i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample & pair_sameclass_w1 == 1, r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Patience and social skills

reg I_patsocial_w2 i.HigherSES##ib0.treatment_w1 I_patsocial_w1M I_patsocial_w1MD $control $FE_pair if $RandomSample , r	
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

reg I_timedisc_w2 i.HigherSES##ib0.treatment_w1 I_timedisc_w1M I_timedisc_w1MD $control $FE_pair if $RandomSample , r	
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

reg I_socialskills_w2 i.HigherSES##ib0.treatment_w1 I_socialskills_w1M I_socialskills_w1MD $control $FE_pair if $RandomSample , r	
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Labor-market orientation

reg I_labororient_w2 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

	


// Table 4: Effect of the Mentoring Program on Index of Labor-Market Prospects for each of the SES Components

reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'
	
reg I_lmprospect_w2 i.HigherSES_books##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r	
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

reg I_lmprospect_w2 i.uni_parents_w1##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r	
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

reg I_lmprospect_w2 i.nonsingleparent_inclstep##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r	
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

reg I_lmprospect_w2 i.nonmigrant_firstGen_w1##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r	
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

	
	
	
// Table 5: Effect of the Mentoring Program on Satisfaction Outcomes

* Life satisfaction
reg sat1_life_w2 i.HigherSES##ib0.treatment_w1 sat1_life_w1M sat1_life_w1MD  $control $FE_pair if $RandomSample, r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Satisfaction with own money and wealth
reg sat1_own_w2 i.HigherSES##ib0.treatment_w1 sat1_own_w1M sat1_own_w1MD  $control $FE_pair if $RandomSample, r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Satisfaction with school situation
reg sat1_school_w2 i.HigherSES##ib0.treatment_w1 sat1_school_w1M sat1_school_w1MD  $control $FE_pair if $RandomSample, r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Satisfaction with perormance in math	
reg sat1_schoolmath_w2 i.HigherSES##ib0.treatment_w1 sat1_schoolmath_w1M sat1_schoolmath_w1MD  $control $FE_pair if $RandomSample, r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'
	

// Table 6: Effect of the Mentoring Program on Early Realizations of Labor-Market Transitions

* Early career realizations
* Doing an apprenticeship
reg AusbildungFB4_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'
	
* Attending school 
reg SchuleFB4_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Work-related activities
reg WorkrelatedFB4_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Unemployed and other activities
reg UnemployothFB4_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* Satisfaction with current situation
* Satisfied
reg sat1_sit_w4 i.HigherSES##ib0.treatment_w1 sat1_life_w1M sat1_life_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

* No desire to change
reg RVchange1_sit_w4 i.HigherSES##ib0.treatment_w1 sat1_life_w1M sat1_life_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r
	sort sortingritest
	ritest treatment_w1 _b, strata(ID_PairsNUM_w1) reps(1000) seed(0): `e(cmdline)'

	
	


/************************************************************************************************************************************************************************************************************************************************

MAIN FIGURES

************************************************************************************************************************************************************************************************************************************************/

// Figure 2: Effect of the Mentoring Program on Labor-Market Prospects

* See specifications in column 4 of Table 2.



// Figure 3: Effect of the Mentoring Program on the Distribution of Labor-Market Prospects
set scheme s2color // to avoid Stata 18 scheme

* Panel A: Distribution of labor-market prospects at baseline

// baseline comparison - w1
gl Y "I_lmprospect_w1"
gl Z "treatment_w1"

*full Sample
gl Sample "$RandomSample"
ksmirnov $Y if $Sample, by(${Z}) e
loc ksmirn_pval = round(r(p_exact),0.001)
disp `ksmirn_pval'
cdfplot $Y if $Sample, by(${Z}) 
escftest $Y if $Sample, group(${Z}) // Epps-Singleton test, better if outcome is discrete. Should have comparable power to KS test
twoway (kdensity $Y if ${Z}==0 & $Sample , legend(label(1 "Control")) lcolor("148 184 228") lpattern(dash)) (kdensity $Y if ${Z}==1 & $Sample , legend(label(2 "Treatment"))  lcolor("31 73 125") lpattern(solid)) , graphregion(color(white)) xtitle("(Baseline) Labor-Market Prospects") ytitle("Density") title("Full sample") yscale(range(0 .5)) ylabel(0 (.1) .5, angle(horizontal)) saving("${figure}\\ks_${Z}_fullw1.gph", replace) text(0.48 -4  "K-S Test p=`ksmirn_pval'", placement(east) size(msmall))

*low SES
gl Sample "$RandomSample & HigherSES==0"
ksmirnov $Y if $Sample, by(${Z}) e
loc ksmirn_pval = round(r(p_exact),0.001)
disp `ksmirn_pval'
cdfplot $Y if $Sample, by(${Z}) 
escftest $Y if $Sample, group(${Z}) // Epps-Singleton test, better if outcome is discrete. Should have comparable power to KS test
twoway (kdensity $Y if ${Z}==0 & $Sample , legend(label(1 "Control")) lcolor("148 184 228") lpattern(dash)) (kdensity $Y if ${Z}==1 & $Sample , legend(label(2 "Treatment"))  lcolor("31 73 125") lpattern(solid)) , graphregion(color(white)) xtitle("(Baseline) Labor-Market Prospects") ytitle("") title("Low-SES sample") yscale(range(0 .5)) ylabel(0 (.1) .5, angle(horizontal)) saving("${figure}\\ks_${Z}_lowSESw1.gph", replace) text(0.48 -4  "K-S Test p=`ksmirn_pval'", placement(east) size(msmall))

*high SES
gl Sample "$RandomSample & HigherSES==1"
ksmirnov $Y if $Sample, by(${Z}) e
loc ksmirn_pval = round(r(p_exact),0.001)
disp `ksmirn_pval'
cdfplot $Y if $Sample, by(${Z}) 
escftest $Y if $Sample, group(${Z}) 
twoway (kdensity $Y if ${Z}==0 & $Sample , legend(label(1 "Control")) lcolor("148 184 228") lpattern(dash)) (kdensity $Y if ${Z}==1 & $Sample , legend(label(2 "Treatment"))  lcolor("31 73 125") lpattern(solid)) , graphregion(color(white)) xtitle("(Baseline) Labor-Market Prospects") ytitle("") title("Higher-SES sample") yscale(range(0 .5)) ylabel(0 (.1) .5, angle(horizontal)) saving("${figure}\\ks_${Z}_highSESw1.gph", replace) text(0.48 -4  "K-S Test p=`ksmirn_pval'", placement(east) size(msmall))

graph combine "${figure}\\ks_${Z}_fullw1.gph" "${figure}\\ks_${Z}_lowSESw1.gph" "${figure}\\ks_${Z}_highSESw1.gph", ///
	row(1) xcom ycom ysize(2) graphregion(margin(none)) plotregion(color(none)) ///
	scale(1.7) imargin(small) saving("${figure}\\ks_${Z}_combined_w1.gph", replace) 


* Panel B: Distribution of labor-market prospects one year after program start

// treatment effects - w2
gl Y "I_lmprospect_w2"
gl Z "treatment_w1"

*full Sample
gl Sample "$RandomSample"
ksmirnov $Y if $Sample, by(${Z}) e
loc ksmirn_pval = round(r(p_exact),0.001)
disp `ksmirn_pval'
cdfplot $Y if $Sample, by(${Z}) 
escftest $Y if $Sample, group(${Z}) 
qui twoway (kdensity $Y if ${Z}==0 & $Sample , legend(label(1 "Control")) lcolor("148 184 228") lpattern(dash)) (kdensity $Y if ${Z}==1 & $Sample , legend(label(2 "Treatment"))  lcolor("31 73 125") lpattern(solid)) , graphregion(color(white)) xtitle("Labor-Market Prospects") ytitle("Density") title("Full sample") yscale(range(0 .5)) ylabel(0 (.1) .5, angle(horizontal)) saving("${figure}\\ks_${Z}_fullw2.gph", replace) text(0.48 -4  "K-S Test p=`ksmirn_pval'", placement(east) size(msmall))
	
*low SES
gl Sample "$RandomSample & HigherSES==0"
ksmirnov $Y if $Sample, by(${Z}) e 
loc ksmirn_pval = round(r(p_exact),0.001)
disp `ksmirn_pval'
cdfplot $Y if $Sample, by(${Z}) 
escftest $Y if $Sample, group(${Z}) 
qui twoway (kdensity $Y if ${Z}==0 & $Sample , legend(label(1 "Control")) lcolor("148 184 228") lpattern(dash)) (kdensity $Y if ${Z}==1 & $Sample , legend(label(2 "Treatment"))  lcolor("31 73 125") lpattern(solid)) , graphregion(color(white)) xtitle("Labor-Market Prospects") ytitle("") title("Low-SES sample") yscale(range(0 .5)) ylabel(0 (.1) .5, angle(horizontal)) saving("${figure}\\ks_${Z}_lowSESw2.gph", replace) text(0.48 -4  "K-S Test p=`ksmirn_pval'", placement(east) size(msmall))

*high SES
gl Sample "$RandomSample & HigherSES==1"
ksmirnov $Y if $Sample, by(${Z}) e
loc ksmirn_pval = round(r(p_exact),0.001)
disp `ksmirn_pval'
cdfplot $Y if $Sample, by(${Z}) 
escftest $Y if $Sample, group(${Z}) 
qui twoway (kdensity $Y if ${Z}==0 & $Sample , legend(label(1 "Control")) lcolor("148 184 228") lpattern(dash)) (kdensity $Y if ${Z}==1 & $Sample , legend(label(2 "Treatment"))  lcolor("31 73 125") lpattern(solid)) , graphregion(color(white)) xtitle("Labor-Market Prospects") ytitle("") title("Higher-SES sample") yscale(range(0 .5)) ylabel(0 (.1) .5, angle(horizontal)) saving("${figure}\\ks_${Z}_highSESw2.gph", replace) text(0.48 -4  "K-S Test p=`ksmirn_pval'", placement(east) size(msmall))

graph combine "${figure}\\ks_${Z}_fullw2.gph" "${figure}\\ks_${Z}_lowSESw2.gph" "${figure}\\ks_${Z}_highSESw2.gph", ///
	row(1) xcom ycom ysize(2) graphregion(margin(none)) plotregion(color(none)) ///
	scale(1.7) imargin(small) saving("${figure}\\ks_${Z}_combined_w2.gph", replace) 


// Figure 4: Share of Treatment Effect for Low-SES Adolescents Attributed to Mediators

* See Section Appendix F: Mediation Analysis further below


// Figure 5: Effect of the Mentoring Program on Components of Labor-Market Prospects for Low-SES Adolescents over Time

* Math achievement
reg math_pre i.HigherSES##ib0.treatment_w1 $control $FE_pair if $RandomSample  & pair_sameclass_w1 == 1 & pair_sameclass_w2 == 1, r
reg math_post1 i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample  & pair_sameclass_w1 == 1 & pair_sameclass_w2 == 1, r
reg math_post2 i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample  & pair_sameclass_w1 == 1 & pair_sameclass_w2 == 1, r
reg math_final i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample  & pair_sameclass_w1 == 1 & pair_sameclass_w2 == 1, r

* Patience
reg I_timedisc_w1 i.HigherSES##ib0.treatment_w1 $control $FE_pair if $RandomSample , r
reg I_timedisc_w2 i.HigherSES##ib0.treatment_w1  I_timedisc_w1M I_timedisc_w1MD $control $FE_pair if $RandomSample , r
reg I_timedisc_w3 i.HigherSES##ib0.treatment_w1  I_timedisc_w1M I_timedisc_w1MD $control $FE_pair i.corona_periods_w3 if $RandomSample , r
reg I_timedisc_w4 i.HigherSES##ib0.treatment_w1  I_timedisc_w1M I_timedisc_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r

* Labor-market orientation

reg I_labororient_w1 i.HigherSES##ib0.treatment_w1 $control $FE_pair if $RandomSample , r 
reg I_labororient_w2 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair if $RandomSample , r 
reg I_labororient_w3 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair i.corona_periods_w3  if $RandomSample , r
reg I_labororient_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair i.corona_periods_w4 if $RandomSample , r 




/************************************************************************************************************************************************************************************************************************************************
APPENDIX
************************************************************************************************************************************************************************************************************************************************/

// Figure A1: Site-specific Effects of the Mentoring Program on Labor-Market Prospects
* Note that we had to suppress the site and school identifiers for data protection reasons. Thus, Appendix Figure A1 and Appendix Table B1 cannot be replicated.

preserve

eststo clear
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample, r
	lincom 1.treatment_w1
	matrix treatstand_lowSES = (0, 0, r(estimate), r(se), r(t), r(p), r(lb), r(ub))
	count if $RandomSample & HigherSES == 0 & e(sample)
	matrix treatstand_lowSES = (treatstand_lowSES, r(N))
	
	lincom 1.treatment_w1 + 1.treatment_w1#1.HigherSES
	matrix treatstand_highSES = (0, 1, r(estimate), r(se), r(t), r(p), r(lb), r(ub))
	count if $RandomSample & e(sample) & HigherSES == 1
	matrix treatstand_highSES = (treatstand_highSES, r(N))	

	matrix treatstand0 = treatstand_lowSES, treatstand_highSES

	
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1##i.standortI_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample, r
	
	* Baseline standort
	lincom 1.treatment_w1
	matrix treatstand_lowSES1003 = (1003, 0, r(estimate), r(se), r(t), r(p), r(lb), r(ub))
	count if $RandomSample & standortI_w1 == 1003 & e(sample) & HigherSES == 0		// Number of Low-SES status
	matrix treatstand_lowSES1003 = (treatstand_lowSES1003, r(N))
	
	lincom 1.treatment_w1 + 1.treatment_w1#1.HigherSES
	matrix treatstand_highSES1003 = (1003, 1, r(estimate), r(se), r(t), r(p), r(lb), r(ub))
	count if $RandomSample & standortI_w1 == 1003 & e(sample) & HigherSES == 1		// Number of Higher-SES status
	matrix treatstand_highSES1003 = (treatstand_highSES1003, r(N))	

	matrix treatstand1003 = treatstand_lowSES1003, treatstand_highSES1003

	
	levelsof standortI_w1 if $RandomSample & standortI_w1 != 1003, local(xx)
	foreach site in `xx'{
	
	lincom 1.treatment_w1 + 1.treatment_w1#`site'.standortI_w1 
	matrix treatstand_lowSES`site' = (`site', 0, r(estimate), r(se), r(t), r(p), r(lb), r(ub))
	count if $RandomSample & standortI_w1 == `site' & e(sample) & HigherSES == 0
	matrix treatstand_lowSES`site' = (treatstand_lowSES`site', r(N))
	
	lincom 1.treatment_w1 + 1.HigherSES#1.treatment_w1 + 1.treatment_w1#`site'.standortI_w1 + 1.HigherSES#1.treatment_w1#`site'.standortI_w1 
	matrix treatstand_highSES`site' = (`site', 1, r(estimate), r(se), r(t), r(p), r(lb), r(ub))
	count if $RandomSample & standortI_w1 == `site' & e(sample) & HigherSES == 1
	matrix treatstand_highSES`site' = (treatstand_highSES`site', r(N))	
	
	matrix treatstand`site' = treatstand_lowSES`site', treatstand_highSES`site'
	}

	matrix treatstand = treatstand0 \ treatstand1003 \ treatstand2000 \ treatstand3355 \ treatstand5113 \ treatstand5315 \ treatstand5334 \ treatstand8222 \ treatstand11000 \ treatstand14713
	matrix list treatstand
	
	
	svmat treatstand
	keep treatstand*
	drop if treatstand1 == .
	drop treatstand2 treatstand10 treatstand11

	rename treatstand1 standortno
	rename treatstand3 treat_estlowSES
	rename treatstand4 treat_selowSES
	rename treatstand5 treat_tvaluelowSES
	rename treatstand6 treat_pvaluelowSES
	rename treatstand7 treat_lblowSES
	rename treatstand8 treat_ublowSES
	rename treatstand9 NlowSES

	rename treatstand12 treat_esthigherSES
	rename treatstand13 treat_sehigherSES
	rename treatstand14 treat_tvaluehigherSES
	rename treatstand15 treat_pvaluehigherSES
	rename treatstand16 treat_lbhigherSES
	rename treatstand17 treat_ubhigherSES
	rename treatstand18 NhigherSES
		
	gen TotalN = NlowSES + NhigherSES
	gen relNlowSES = NlowSES / TotalN

	reshape long treat_est treat_se treat_tvalue treat_pvalue treat_lb treat_ub N, j(lowSES, string) i(standortno) 
	replace lowSES = "0" if lowSES == "higherSES"
	replace lowSES = "1" if lowSES == "lowSES"
	destring lowSES, replace
	la def lowSES 1 "Low-SES" 0 "Higher-SES"
	la val lowSES lowSES
	
	gsort lowSES -treat_est
	bysort lowSES: gen running=_n

	gen label = -1.8
	gen label2 = -2.2

	
	* Low SES
	#delimit ;
	graph twoway bar treat_est running if lowSES == 1 & standortno != 0, color(gray) lcolor(gray) fcolor(gray) ||
	bar treat_est running if lowSES == 1  & standortno == 0, color(blue) lcolor(blue) fcolor(blue) ||	
	rcap treat_lb treat_ub running if lowSES == 1, lcolor(black) ||
	scatter label running  if lowSES == 1, mlabel(TotalN) mlabposition(0) mlabcolor(black) m(i) ||
	scatter label2 running  if lowSES == 1, mlabel(relNlowSES) mlabformat(%9.2f) mlabposition(0) mlabcolor(black) m(i)
	graphregion(color(white))
	xlabel(1 "Duisburg-Essen" 2 "Lueneburg" 3 "Hamburg" 4 "Leipzig" 5 "Aachen" 6 "Mannheim" 7 "Luebeck" 8 "Average" 9 "Berlin" 10 "Cologne" , angle(60))	
	xtitle("Site")
	ytitle("Treatment effect (SD)")
	yscale(range(-2.4)) ylabel(-1.5 "-1.5" -1 "-1.0" -0.5 "-0.5" 0 "0" 0.5 "0.5" 1 "1.0" 1.5 "1.5" 2.0 "2.0" 2.5 "2.5" 3.0 "3.0", angle(0))
	legend(order(1 3) label(1 "Treatment effect Low-SES") label(3 "95% CI"))
	note("1st (2nd) number below bars: number in estimation sample (percent low-SES)")
	;
	#delimit cr

restore

	
// Table A1: Comparison of Sample to German Student Population in PISA

preserve
	
keep if $RandomSample

replace highed_f_w1 = 9 if missing(highed_f_w1) 
tab highed_f_w1, gen(dhighed_f_w1) 

replace highed_m_w1 = 9 if missing(highed_m_w1) 
tab highed_m_w1 , gen(dhighed_m_w1)

replace uni_parents_w1 = 9 if highed_f_w1==9 & highed_m_w1==9
tab uni_parents_w1 , gen(duni_parents_w1)

replace empl_fSES = 9 if empl_fSES ==9 |  missing(empl_fSES)
tab empl_fSES, gen(dempl_fSES)

replace empl_mSES = 9 if empl_mSES ==9 |  missing(empl_mSES)
tab empl_mSES, gen(dempl_mSES)

tab books_w1, gen(dbooks_w1)

rename migrant_firstGen_w1 migrant_first
rename singleparent_inclstep singleparent

expand 2, gen(samples)

replace samples = 2 if samples == 1 & HigherSES == 1


#delimit;
local varlist LowSES dbooks_w11 dbooks_w12 dbooks_w13 dbooks_w14 dbooks_w15 dbooks_w16 migrant_w1 migrant_first age_w1 male_w1  singleparent
dhighed_f_w11 dhighed_f_w12 dhighed_f_w13 dhighed_m_w11 dhighed_m_w12 dhighed_m_w13 duni_parents_w11 duni_parents_w12 duni_parents_w13 dempl_fSES1 dempl_fSES2 dempl_fSES3 dempl_fSES4 dempl_fSES5 dempl_mSES1 dempl_mSES2 dempl_mSES3 dempl_mSES4 dempl_mSES5
;
#delimit cr

foreach xx in `varlist'{
    rename `xx' mean`xx'
}

foreach xx in `varlist'{
    gen sd`xx' = mean`xx'
	gen obs`xx' = mean`xx'
}


#delimit;
local meanvarlist meanLowSES meandbooks_w11 meandbooks_w12 meandbooks_w13 meandbooks_w14 meandbooks_w15 meandbooks_w16 meanmigrant_w1 meanmigrant_first meanage_w1 meanmale_w1 meansingleparent meandhighed_f_w11 meandhighed_f_w12 meandhighed_f_w13 meandhighed_m_w11 meandhighed_m_w12 meandhighed_m_w13 meanduni_parents_w11 meanduni_parents_w12 meanduni_parents_w13 meandempl_fSES1 meandempl_fSES2 meandempl_fSES3 meandempl_fSES4 meandempl_fSES5 meandempl_mSES1 meandempl_mSES2 meandempl_mSES3 meandempl_mSES4 meandempl_mSES5;
local sdvarlist sdLowSES sddbooks_w11 sddbooks_w12 sddbooks_w13 sddbooks_w14 sddbooks_w15 sddbooks_w16 sdmigrant_w1 sdmigrant_first sdage_w1 sdmale_w1  sdsingleparent
sddhighed_f_w11 sddhighed_f_w12 sddhighed_f_w13 sddhighed_m_w11 sddhighed_m_w12 sddhighed_m_w13 sdduni_parents_w11 sdduni_parents_w12 sdduni_parents_w13 sddempl_fSES1 sddempl_fSES2 sddempl_fSES3 sddempl_fSES4 sddempl_fSES5 sddempl_mSES1 sddempl_mSES2 sddempl_mSES3 sddempl_mSES4 sddempl_mSES5;
local obsvarlist obsLowSES obsdbooks_w11 obsdbooks_w12 obsdbooks_w13 obsdbooks_w14 obsdbooks_w15 obsdbooks_w16 obsmigrant_w1 obsmigrant_first obsage_w1 obsmale_w1 obssingleparent obsdhighed_f_w11 obsdhighed_f_w12 obsdhighed_f_w13 obsdhighed_m_w11 obsdhighed_m_w12 obsdhighed_m_w13 obsduni_parents_w11 obsduni_parents_w12 obsduni_parents_w13 obsdempl_fSES1 obsdempl_fSES2 obsdempl_fSES3 obsdempl_fSES4 obsdempl_fSES5 obsdempl_mSES1 obsdempl_mSES2 obsdempl_mSES3 obsdempl_mSES4 obsdempl_mSES5;
#delimit cr

#delimit;
collapse (mean) `meanvarlist' (sd) `sdvarlist' (count) `obsvarlist', by(samples)
;
#delimit cr


reshape long mean sd obs, i(samples) j(variable, string)
reshape wide mean sd obs, i(variable) j(samples)

replace variable = "Low-SES" if variable == "LowSES"
replace variable = "age" if variable == "age_w1"
replace variable = "books_010" if variable == "dbooks_w11"
replace variable = "books_1125" if variable == "dbooks_w12"
replace variable = "books_26100" if variable == "dbooks_w13"
replace variable = "books_101200" if variable == "dbooks_w14"
replace variable = "books_201500" if variable == "dbooks_w15"
replace variable = "books_m500" if variable == "dbooks_w16"
replace variable = "empl_f_unemployed" if variable == "dempl_fSES1"
replace variable = "empl_f_notemployedothers" if variable == "dempl_fSES2"
replace variable = "empl_f_parttime" if variable == "dempl_fSES3"
replace variable = "empl_f_fulltime" if variable == "dempl_fSES4"
replace variable = "empl_f_missing" if variable == "dempl_fSES5"
replace variable = "empl_m_unemployed" if variable == "dempl_mSES1"
replace variable = "empl_m_notemployedothers" if variable == "dempl_mSES2"
replace variable = "empl_m_parttime" if variable == "dempl_mSES3"
replace variable = "empl_m_fulltime" if variable == "dempl_mSES4"
replace variable = "empl_m_missing" if variable == "dempl_mSES5"
replace variable = "uni_f_no" if variable == "dhighed_f_w11"
replace variable = "uni_f_yes" if variable == "dhighed_f_w12"
replace variable = "uni_f_missing" if variable == "dhighed_f_w13"
replace variable = "uni_m_no" if variable == "dhighed_m_w11"
replace variable = "uni_m_yes" if variable == "dhighed_m_w12"
replace variable = "uni_m_missing" if variable == "dhighed_m_w13"
replace variable = "uni_parents_no" if variable == "duni_parents_w11"
replace variable = "uni_parents_yes" if variable == "duni_parents_w12"
replace variable = "uni_parents_missing" if variable == "duni_parents_w13"
replace variable = "male" if variable == "male_w1"
replace variable = "migrant" if variable == "migrant_w1"

merge 1:1 variable using "${data}\PISA2012_Averages.dta"
drop _merge

format mean* sd* %9.5fc

* Tests and tables

replace variable = "low_SES" if variable == "Low-SES"

#delimit;
local low low_SES;
local varlist
books_010
books_1125
books_26100
books_101200
books_201500
books_m500
male
age 
migrant
migrant_first
singleparent
uni_f_yes
uni_f_no
uni_f_missing
uni_m_yes
uni_m_no
uni_m_missing
uni_parents_yes
uni_parents_no
uni_parents_missing
empl_f_fulltime
empl_f_parttime
empl_f_notemployedothers
empl_f_unemployed
empl_f_missing
empl_m_fulltime
empl_m_parttime
empl_m_notemployedothers
empl_m_unemployed
empl_m_missing;
local varlists `low' `varlist'
;
#delimit cr


foreach var in `varlists'{
forval s = 0/2{

sca drop _all
sum mean`s' if variable == "`var'"
	sca meanRYLSample`s' = r(mean)
sum sd`s' if variable == "`var'"
	sca sdRYLSample`s' = r(mean)
sum obs`s' if variable == "`var'"
	sca obsRYLSample`s' = r(mean)

sum meanPISA`s' if variable == "`var'"
	sca meanPISASample`s' = r(mean)
sum sdPISA`s' if variable == "`var'"
	sca sdPISASample`s' = r(mean)
sum obsPISA`s' if variable == "`var'"
	sca obsPISASample`s' = r(mean)
		
ttesti `=obsRYLSample`s'' `=meanRYLSample`s'' `=sdRYLSample`s'' `=obsPISASample`s'' `=meanPISASample`s'' `=sdPISASample`s''
	sca de p1 = r(p)
	matrix M`var'`s' = (`=meanRYLSample`s'' , `=meanRYLSample`s''-`=meanPISASample`s'', p1)
}
}
*

foreach var in `varlists'{  
	matrix M`var' = (M`var'0, M`var'1, M`var'2)
}

matrix M = Mlow_SES
foreach var in `varlist'{  
	matrix M = M \ M`var'
}

matrix list M


* Compare low-SES and higher-SES to PISA average
foreach var in `varlists'{
forval s = 0/2{

sca drop _all
sum mean`s' if variable == "`var'"
	sca meanRYLSample`s' = r(mean)
sum sd`s' if variable == "`var'"
	sca sdRYLSample`s' = r(mean)
sum obs`s' if variable == "`var'"
	sca obsRYLSample`s' = r(mean)

sum meanPISA0 if variable == "`var'"
	sca meanPISASample0 = r(mean)
sum sdPISA0 if variable == "`var'"
	sca sdPISASample0 = r(mean)
sum obsPISA0 if variable == "`var'"
	sca obsPISASample0 = r(mean)
		
ttesti `=obsRYLSample`s'' `=meanRYLSample`s'' `=sdRYLSample`s'' `=obsPISASample0' `=meanPISASample0' `=sdPISASample0'
	sca de p1 = r(p)
	matrix MPa`var'`s' = (`=meanRYLSample`s'' , `=meanRYLSample`s''-`=meanPISASample0', p1)
}
}
*

foreach var in `varlists'{  
	matrix MPa`var' = (MPa`var'0, MPa`var'1, MPa`var'2)
}

matrix MPa = MPalow_SES
foreach var in `varlist'{  
	matrix MPa = MPa \ MPa`var'
}

matrix list MPa

matrix M = (M, MPa) 



clear
svmat M


rename M1 Mean_All_RYL
rename M2 Diff_PISA2012_ALL
rename M3 Diffpval_PISA2012_ALL
rename M4 Mean_low_RYL
rename M5 Diff_PISA2012_low
rename M6 Diffpval_PISA2012_low
rename M7 Mean_higher_RYL
rename M8 Diff_PISA2012_higher
rename M9 Diffpval_PISA2012_higher

drop M10 M11 M12 M13 M16

rename M14 Diff_PISA2020_ALL_lowSES
rename M15 Diffpval_PISA2020_ALL_lowSES

rename M17 Diff_PISA2020_ALL_higherSES
rename M18 Diffpval_PISA2020_ALL_higherSES

format Mean* Diff* %9.2fc

gen variable = ""
order variable
replace variable = "Low-SES" in 1
replace variable = "Books: 0-10" in 2
replace variable = "Books: 11-25" in 3
replace variable = "Books: 26-100" in 4
replace variable = "Books: 101-200" in 5
replace variable = "Books: 201-500" in 6
replace variable = "Books: More than 500" in 7
replace variable = "Male" in 8
replace variable = "Age" in 9 
replace variable = "Migrant" in 10
replace variable = "First-generation migrant" in 11
replace variable = "Single-parent status" in 12
replace variable = "University degree, father: yes" in 13
replace variable = "University degree, father: no" in 14
replace variable = "University degree, father: missing" in 15
replace variable = "University degree, mother: yes" in 16
replace variable = "University degree, mother: no" in 17
replace variable = "University degree, mother: missing" in 18
replace variable = "University degree, parents: yes" in 19
replace variable = "University degree, parents: no" in 20
replace variable = "University degree, parents: missing" in 21
replace variable = "Employment, father: fulltime" in 22
replace variable = "Employment, father: parttime" in 23
replace variable = "Employment, father: not employed, not searching" in 24
replace variable = "Employment, father: unemployed" in 25
replace variable = "Employment, father: missing" in 26
replace variable = "Employment, mother: fulltime" in 27
replace variable = "Employment, mother: parttime" in 28
replace variable = "Employment, mother: not employed, not searching" in 29
replace variable = "Employment, mother: unemployed" in 30
replace variable = "Employment, mother: missing" in 31

restore


// Table A2: Balancing in Subsamples

preserve
	loc dep_OutcomesSummary 	I_lmprospect_w1 math_pre math_preMD I_patsocial_w1 I_labororient_w1
	loc dep_Outcomes 			I_timedisc_w1 I_socialskills_w1 I_prosoc_w1 I_trust_w1 I_selfeff_w1 wish_appr_w1 ZZknowcareerfut_w1
	loc Vars_bal 				male_w1 age_w1 migrant_w1 books_gr_w1 grade_math_gr_w1 grade_math_gr_w1MD grade_german_gr_w1 grade_german_gr_w1MD grade_english_gr_w1 grade_english_w1MD privatteach_w1 parentssupport_w1 bfi_con_w1 bfi_neur_w1
	loc Vars_control 			bfi_open_w1 bfi_extra_w1 bfi_agree_w1 
	loc Vars_SES		 	    HigherSES 

	loc j=1
	foreach covariates in "" "" {
		loc i=1
		foreach IF_sample in "if Pairs_NoTwin_w1==0 & HigherSES==0" "if Pairs_NoTwin_w1==0 & HigherSES==1" {
			disp as err `i'
			loc name1 "low-SES sample"
			loc name2 "higher-SES sample"
			disp as err "`name`i''"
			orth_out `dep_OutcomesSummary' 			using "${results}\\TableA2-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') replace bdec(3) title(Main outcomes baseline (summary measures)) 
			orth_out `dep_Outcomes' 				using "${results}\\TableA2-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Main outcomes baseline (components))
			noi cap orth_out `Vars_bal' 			using "${results}\\TableA2-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Matching variables randomization baseline)
			noi cap orth_out `Vars_control'		 	using "${results}\\TableA2-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Further control variables baseline)  
			noi cap orth_out `Vars_SES'		 		using "${results}\\TableA2-`name`i''-`covariates'_HigherSES.xlsx" `IF_sample', by(treatment_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Further SES variables baseline) notes(Sample: `name`i''; `covariates') count 
			loc ++i	
		}
		loc ++j
	}
	beep
restore

******

** TESTING DIFFERENCES BETWEEN LOW-SES AND HIGH-SES (Table A2: Balancing in Subsamples, last column)

preserve
	loc dep_OutcomesSummary 	I_lmprospect_w1 math_pre math_preMD I_patsocial_w1 I_labororient_w1
	loc dep_Outcomes 			I_timedisc_w1 I_socialskills_w1 I_prosoc_w1 I_trust_w1 I_selfeff_w1 wish_appr_w1 ZZknowcareerfut_w1
	loc Vars_bal 				male_w1 age_w1 migrant_w1 books_gr_w1 grade_math_gr_w1 grade_math_gr_w1MD grade_german_gr_w1 grade_german_gr_w1MD grade_english_gr_w1 grade_english_w1MD privatteach_w1 parentssupport_w1 bfi_con_w1 bfi_neur_w1
	loc Vars_control 			bfi_open_w1 bfi_extra_w1 bfi_agree_w1 
	loc Vars_SES		 	    HigherSES 
	
	loc j=1
	foreach covariates in "" "" {
		loc i=1
		foreach IF_sample in "if Pairs_NoTwin_w1==0 & treatment_w1==0" {
			disp as err `i'
			loc name1 "control group"
			disp as err "`name`i''"
			orth_out `dep_OutcomesSummary' 			using "${results}\\TableA2_Test_Control_Low_vs_HigherSES.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') replace bdec(3) title(Main outcomes baseline (summary measures)) 
			orth_out `dep_Outcomes' 				using "${results}\\TableA2_Test_Control_Low_vs_HigherSES.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Main outcomes baseline (components))
			noi cap orth_out `Vars_bal' 			using "${results}\\TableA2_Test_Control_Low_vs_HigherSES.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Matching variables randomization baseline)
			noi cap orth_out `Vars_control'		 	using "${results}\\TableA2_Test_Control_Low_vs_HigherSES.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Further control variables baseline)  
			noi cap orth_out `Vars_SES'		 		using "${results}\\TableA2_Test_Control_Low_vs_HigherSES.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Further SES variables baseline) notes(Sample: `name`i''; `covariates') count 			
			loc ++i	
		}
		loc ++j
	}
	beep
restore
	
	
	


// Table A4: Overall Effect of the Mentoring Program: Splitting the Sample by SES

* A. With randomization-pair fixed effects
reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control if $RandomSample & HigherSES == 0, r
reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control if $RandomSample & HigherSES == 1, r

reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control if $RandomSample & pair_higherSES == 0, r
reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control if $RandomSample & pair_higherSES == 1, r


* B. With randomization-pair fixed effects
reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & HigherSES == 0, r
reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & HigherSES == 1, r

reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & pair_higherSES == 0, r
reg I_lmprospect_w2 i.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & pair_higherSES == 1, r


// Table A5: Effect of the Mentoring Program by Gender

reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r
reg I_lmprospect_w2 i.female_w1##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 i.female_w1##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r


// Table A6: Effect of the Mentoring Program on the Three Components of the Index of Labor-Market Prospects: Correction for Multiple Hypotheses Testing

* shorter variable names for outcomes s.t. rwolf command does not return error message
clonevar mathe_2 = math_post1
clonevar NonCogn_w2 = I_patsocial_w2
clonevar labororient_w2 = I_labororient_w2

* create baseline outcomes s.t. rwolf can understand them
clonevar mathe_2_bl = math_preM
clonevar NonCogn_w2_bl = I_patsocial_w1M
clonevar labororient_w2_bl = I_labororient_w1M

** baseline p-values

preserve

keep if $RandomSample 

gen treat_books = treatment_w1 * HigherSES

reg mathe_2 treatment_w1 treat_books HigherSES $control math_preM math_preMD $FE_pair, r
reg NonCogn_w2 treatment_w1 treat_books HigherSES $control I_patsocial_w1M I_patsocial_w1MD $FE_pair, r
reg labororient_w2 treatment_w1 treat_books HigherSES $control I_labororient_w1M I_labororient_w1MD $FE_pair,  r

restore

****

** 1.) mhtreg (by John List, Azeem Shaikh and Yang Xu (2016))

preserve

keep if $RandomSample 

gen treat_books = treatment_w1 * HigherSES

* low-SES
mhtreg (mathe_2 treatment_w1 treat_books HigherSES $control math_preM math_preMD $FE_pair) /// 
(NonCogn_w2 treatment_w1 treat_books HigherSES $control I_patsocial_w1M I_patsocial_w1MD $FE_pair) /// 
(labororient_w2 treatment_w1 treat_books HigherSES $control I_labororient_w1M I_labororient_w1MD $FE_pair) /// 
, cluster($FEabsorb_pair) cltype(1) bootstrap(1000) seed(124) 

* higher-SES
mhtreg (mathe_2 treat_books treatment_w1 HigherSES $control math_preM math_preMD $FE_pair) /// 
(NonCogn_w2 treat_books treatment_w1 HigherSES $control I_patsocial_w1M I_patsocial_w1MD $FE_pair) /// 
(labororient_w2 treat_books treatment_w1 HigherSES $control I_labororient_w1M I_labororient_w1MD $FE_pair) /// 
, cluster($FEabsorb_pair) cltype(1) bootstrap(1000) seed(124)

restore

****

** 2.) wyoung

preserve

keep if $RandomSample 

gen treat_books = treatment_w1 * HigherSES

* low-SES
wyoung, cmd("reg mathe_2 treatment_w1 treat_books HigherSES $control math_preM math_preMD $FE_pair, r" ///
"reg NonCogn_w2 treatment_w1 treat_books HigherSES $control I_patsocial_w1M I_patsocial_w1MD $FE_pair, r" ///
"reg labororient_w2 treatment_w1 treat_books HigherSES $control I_labororient_w1M I_labororient_w1MD $FE_pair, r") ///
cluster($FEabsorb_pair) familyp(treatment_w1) bootstraps(1000) seed(124)

* higher-SES
wyoung, cmd("reg mathe_2 treat_books treatment_w1 HigherSES $control math_preM math_preMD $FE_pair, r" ///
"reg NonCogn_w2 treat_books treatment_w1 HigherSES $control I_patsocial_w1M I_patsocial_w1MD $FE_pair, r" ///
"reg labororient_w2 treat_books treatment_w1 HigherSES $control I_labororient_w1M I_labororient_w1MD $FE_pair,  r") ///
cluster($FEabsorb_pair) familyp(treat_books) bootstraps(1000) seed(124)

restore


// Table A7: Effect of the Mentoring Program on Measures of Social Capital

reg volunteer_w2 i.HigherSES##ib0.treatment_w1 volunteer_w1M volunteer_w1MD $control $FE_pair if $RandomSample , r
reg I_friends_w2 i.HigherSES##ib0.treatment_w1 I_friends_w1M I_friends_w1MD $control $FE_pair if $RandomSample , r
reg I_meetfriends_w2 i.HigherSES##ib0.treatment_w1 I_meetfriends_w1M I_meetfriends_w1MD $control $FE_pair if $RandomSample , r	
reg I_schoolact_w2 i.HigherSES##ib0.treatment_w1 $control $FE_pair if $RandomSample , r
reg I_schoolacthigh_w2 i.HigherSES##ib0.treatment_w1 $control $FE_pair if $RandomSample , r
reg I_schoolactlow_w2 i.HigherSES##ib0.treatment_w1 $control $FE_pair if $RandomSample , r



// Table A8: Effect of the Mentoring Program on Plans after School

reg wish_appr_w2 i.HigherSES##ib0.treatment_w1 wish_appr_w1M wish_appr_w1MD $control $FE_pair if $RandomSample , r
reg wish_study_w2 i.HigherSES##ib0.treatment_w1 wish_study_w1M wish_study_w1MD $control $FE_pair if $RandomSample , r
reg wish_dontknow_w2 i.HigherSES##ib0.treatment_w1 wish_dontknow_w1M wish_dontknow_w1MD $control $FE_pair if $RandomSample , r
reg wish_job_w2 i.HigherSES##ib0.treatment_w1 wish_job_w1M wish_job_w1MD $control $FE_pair if $RandomSample , r
reg wish_other_w2 i.HigherSES##ib0.treatment_w1 wish_other_w1M wish_other_w1MD $control $FE_pair if $RandomSample , r



// Table A9: Effect of the Mentoring Program by Migrant Status: First- and Second-Generation Migrants

reg I_lmprospect_w2 i.nonmigrant_w1##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r
reg I_lmprospect_w2 i.nonmigrant_w1##ib0.treatment_w1 i.migrant_secondGen_w1##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample , r

reg math_post1 i.nonmigrant_w1##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample , r
reg math_post1 i.nonmigrant_w1##ib0.treatment_w1 i.migrant_secondGen_w1##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample , r

reg I_patsocial_w2 i.nonmigrant_w1##ib0.treatment_w1 I_patsocial_w1M I_patsocial_w1MD $control $FE_pair if $RandomSample , r
reg I_patsocial_w2 i.nonmigrant_w1##ib0.treatment_w1 i.migrant_secondGen_w1##ib0.treatment_w1 I_patsocial_w1M I_patsocial_w1MD $control $FE_pair if $RandomSample , r

reg I_labororient_w2 i.nonmigrant_w1##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair if $RandomSample , r
reg I_labororient_w2 i.nonmigrant_w1##ib0.treatment_w1 i.migrant_secondGen_w1##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair if $RandomSample , r



// Table A10: Evidence on the Mentoring Relationships

preserve

keep if treatment_w1==1 & $RandomSample

replace stillmentor_w2=0 if stillmentor_w2==.

gen stillmentor_if_participated_w2=stillmentor_w2 if participated_w2==1
label var stillmentor_if_participated_w2 "Mentoring relationship still exists (if mentor/mentee ever met)"

gen contact_personCOARSE_month_w2=0
replace contact_personCOARSE_month_w2=1 if ///
(contact_personCOARSE_w2==1|contact_personCOARSE_w2==2|contact_personCOARSE_w2==3|contact_personCOARSE_w2==4)
replace contact_personCOARSE_month_w2=. if missing(contact_personCOARSE_w2)
label var contact_personCOARSE_month_w2 "Meet at least once per month (in person)"

gen contact_digitalCOARSE_month_w2=0
replace contact_digitalCOARSE_month_w2=1 if ///
(contact_phoneCOARSE_w2==1|contact_phoneCOARSE_w2==2|contact_phoneCOARSE_w2==3|contact_phoneCOARSE_w2==4)| ///
(contact_messageCOARSE_w2==1|contact_messageCOARSE_w2==2|contact_messageCOARSE_w2==3|contact_messageCOARSE_w2==4)| ///
(contact_mailCOARSE_w2==1|contact_mailCOARSE_w2==2|contact_mailCOARSE_w2==3|contact_mailCOARSE_w2==4)| ///
(contact_otherCOARSE_w2==1|contact_otherCOARSE_w2==2|contact_otherCOARSE_w2==3|contact_otherCOARSE_w2==4)
replace contact_digitalCOARSE_month_w2=. if missing(contact_phoneCOARSE_w2)&missing(contact_messageCOARSE_w2)&missing(contact_mailCOARSE_w2)&missing(contact_otherCOARSE_w2)
label var contact_digitalCOARSE_month_w2 "Meet at least once per month (digital)"

gen contact_allCOARSE_month_w2=0
replace contact_allCOARSE_month_w2=1 if ///
(contact_personCOARSE_w2==1|contact_personCOARSE_w2==2|contact_personCOARSE_w2==3|contact_personCOARSE_w2==4)| ///
(contact_phoneCOARSE_w2==1|contact_phoneCOARSE_w2==2|contact_phoneCOARSE_w2==3|contact_phoneCOARSE_w2==4)| ///
(contact_messageCOARSE_w2==1|contact_messageCOARSE_w2==2|contact_messageCOARSE_w2==3|contact_messageCOARSE_w2==4)| ///
(contact_mailCOARSE_w2==1|contact_mailCOARSE_w2==2|contact_mailCOARSE_w2==3|contact_mailCOARSE_w2==4)| ///
(contact_otherCOARSE_w2==1|contact_otherCOARSE_w2==2|contact_otherCOARSE_w2==3|contact_otherCOARSE_w2==4)
replace contact_allCOARSE_month_w2=. if missing(contact_personCOARSE_w2)&missing(contact_phoneCOARSE_w2)&missing(contact_messageCOARSE_w2)&missing(contact_mailCOARSE_w2)&missing(contact_otherCOARSE_w2)
label var contact_allCOARSE_month_w2 "Meet at least once per month (all channels)"

* relationship assessment variables as dummies
foreach var of newlist relation_decide relation_rolem relation_satisfied relation_choose relation_parents relation_friends relation_close relation_betterschool relation_helpproblem {
gen `var'_d_w2=0
replace `var'_d_w2=1 if `var'_w2>3 
replace `var'_d_w2=. if missing(`var'_w2)
replace `var'_d_w2=0 if `var'_w2==.n //Don't know / can't tell(9) --> conservatively, we assign a 0 if mentees could not answer this question 
tab `var'_d_w2, m
}

label var relation_decide_d_w2 "Mentee can decide what is done in meetings"
label var relation_rolem_d_w2 "Mentor is role model"
label var relation_satisfied_d_w2 "Mentee satisfied with mentoring relationship"
label var relation_choose_d_w2 "Mentee had a say in which mentor he/she got"
label var relation_parents_d_w2 "Parents support mentee having a mentor"
label var relation_friends_d_w2 "Friends support mentee having a mentor"
label var relation_close_d_w2 "Mentee and mentor are good friends"
label var relation_betterschool_d_w2 "Mentee better at school because of mentor"
label var relation_helpproblem_d_w2 "Mentor helped solving non-school-related problems"
		
	loc Basic 			participated_w2 stillmentor_w2 stillmentor_if_participated_w2 
	loc Frequency 		contact_personCOARSE_month_w2 contact_allCOARSE_month_w2 duration_w2 	
	loc Talkmentor 		talkmentor_school_w2 talkmentor_hobby_w2 talkmentor_future_w2 talkmentor_jobfuture_w2 talkmentor_person_w2  talkmentor_family_w2 talkmentor_other_w2 talkmentor_dontknow_w2 relation_decide_d_w2
	loc Relation 		relation_betterschool_d_w2 relation_helpproblem_d_w2 relation_rolem_d_w2 relation_parents_d_w2 relation_friends_d_w2 relation_choose_d_w2  relation_close_d_w2  relation_satisfied_d_w2
	
		loc j=1
	foreach covariates in "" "" {
		loc i=1
		foreach IF_sample in "if Pairs_NoTwin_w1==0" {
			disp as err `i'
			loc name1 "full sample"
			disp as err "`name`i''"
			orth_out `Basic' 				using "${results}\\TableA10.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') replace bdec(3) title(Initiation and continuation of relationship) 
			orth_out `Frequency' 			using "${results}\\TableA10.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Meeting frequency and duration)			
			orth_out `Talkmentor' 			using "${results}\\TableA10.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Topics discussed during meetings)
			orth_out `Relation'		 		using "${results}\\TableA10.xlsx" `IF_sample', by(HigherSES) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Qualitative factors of relationship) notes(Sample: `name`i''; `covariates') count 
			loc ++i
		}
		loc ++j
	}
	beep
	
restore


// Table A11: Effect of the Mentoring Program on School Performance

reg math_post1 i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample & pair_sameclass_w1 == 1 & pair_sameclass_w2 == 1, r
reg math_post2 i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample & pair_sameclass_w1 == 1 & pair_sameclass_w2 == 1, r
reg math_final i.HigherSES##ib0.treatment_w1 math_preM math_preMD $control $FE_pair if $RandomSample & pair_sameclass_w1 == 1 & pair_sameclass_w2 == 1, r

reg graduation_all i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample, r
reg graduation_upper i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample, r


// Table A12: Effect of the Mentoring Program on Career Vision

* person has apprenticeship
gen has_vet=0  
replace has_vet=1 if AusbildungFB4_w4==1
label var has_vet "Has apprenticeship"

* person knows with "high" or "very high" probability which occupation he/she wants to work in
gen has_wish=0  
replace has_wish=1 if !missing(wish1_kldb)|!missing(wish2_kldb)|!missing(wish3_kldb)
label var has_wish "Has desired occuation"

* person knows which occupation he/she want to work in and/or has apprenticeship
gen has_vet_or_wish=0
replace has_vet_or_wish=1 if has_vet==1|has_wish==1
replace has_vet_or_wish=. if missing(has_vet)&missing(has_wish)
label var has_vet "Has desired or apprenticeship occupation"
				
reg has_wish i.HigherSES##ib0.treatment_w1 $control $FE_pair I_labororient_w1M I_labororient_w1MD i.corona_periods_w4 if $RandomSample, r
reg has_vet_or_wish i.HigherSES##ib0.treatment_w1 $control $FE_pair I_labororient_w1M I_labororient_w1MD i.corona_periods_w4 if $RandomSample, r


// Table A13: Effect of the Mentoring Program on Successful Completion and Expected Labor-Market Returns

reg prob_studium_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair  i.corona_periods_w4 if $RandomSample , r
reg prob_lehre_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair  i.corona_periods_w4 if $RandomSample , r

reg return_studium_trim_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair  i.corona_periods_w4 if $RandomSample , r
reg return_lehre_trim_w4 i.HigherSES##ib0.treatment_w1 I_labororient_w1M I_labororient_w1MD $control $FE_pair  i.corona_periods_w4 if $RandomSample , r



******************************************************************************************************************
* Appendix B: Survey Frame: Sites, Cohorts, and Timing
*******************************************************************************************************************

// Table B1: Observations in the Baseline Survey by Mentoring Site and Cohort
* Note that we had to suppress the site and school identifiers for data protection reasons. Thus, Appendix Figure A1 and Appendix Table B1 cannot be replicated.

preserve
gen obs = 1

collapse (sum) obs, by(ID_City_w1 ID_School_w1  cohort  ID_CitySchoolCohort_w1 treatment_w1 Pairs_NoTwin_w1)
reshape wide obs, i(ID_City_w1 ID_School_w1  cohort ID_CitySchoolCohort_w1 treatment_w1) j(Pairs_NoTwin_w1)

rename obs0 obs_random
rename obs1 obs_nonrandom
 
reshape wide obs_random obs_nonrandom, i(ID_CitySchoolCohort_w1) j(treatment_w1)

rename obs_random0 C_randomsample
rename obs_random1 T_randomsample

rename obs_nonrandom0 C_nonrandomsample
rename obs_nonrandom1 T_nonrandomsample

order ID_CitySchoolCohort_w1 cohort ID_City_w1 ID_School_w1  C_randomsample T_randomsample C_nonrandomsample T_nonrandomsample
sort cohort ID_City_w1 ID_School_w1

replace ID_City_w1 = "Aachen" if ID_City_w1 == "AA"
replace ID_City_w1 = "Berlin" if ID_City_w1 == "BE"
replace ID_City_w1 = "Duisburg" if ID_City_w1 == "DE"
replace ID_City_w1 = "Essen" if ID_City_w1 == "ES"
replace ID_City_w1 = "Hamburg" if ID_City_w1 == "HH"
replace ID_City_w1 = "Cologne" if ID_City_w1 == "KO"
replace ID_City_w1 = "Lueneburg" if ID_City_w1 == "LN"
replace ID_City_w1 = "Luebeck" if ID_City_w1 == "LU"
replace ID_City_w1 = "Mannheim" if ID_City_w1 == "MA"
replace ID_City_w1 = "Chemnitz" if ID_City_w1 == "CH"
replace ID_City_w1 = "Leipzig" if ID_City_w1 == "LE"
replace ID_City_w1 = "Bonn" if ID_City_w1 == "BO"

gen school = substr(ID_School_w1,4,1)
destring school, replace
drop ID_School_w1


gen befragung_year = substr(ID_CitySchoolCohort_w1,8,2)
replace befragung_year = "2015" if befragung_year == "15"
replace befragung_year = "2016" if befragung_year == "16"
replace befragung_year = "2017" if befragung_year == "17"
replace befragung_year = "2018" if befragung_year == "18"

gen befragung_month = substr(ID_CitySchoolCohort_w1,10,2)

drop ID_CitySchoolCohort_w1
sort cohort ID_City_w1 school 

egen C_complete = rowtotal(C_randomsample C_nonrandomsample)
egen T_complete = rowtotal(T_randomsample T_nonrandomsample)
drop C_nonrandomsample T_nonrandomsample 

order cohort ID_City_w1 school befragung_year befragung_month C_complete T_complete C_randomsample T_randomsample
restore




// Table B2: Comparison of Randomized and Non-Randomized Samples

preserve
	loc dep_OutcomesSummary 	I_lmprospect_w1 math_pre math_preMD I_patsocial_w1 I_labororient_w1
	loc dep_Outcomes 			I_timedisc_w1 I_socialskills_w1 I_prosoc_w1 I_trust_w1 I_selfeff_w1 wish_appr_w1 ZZknowcareerfut_w1
	loc Vars_bal 				male_w1 age_w1 migrant_w1 books_gr_w1 grade_math_gr_w1 grade_math_gr_w1MD grade_german_gr_w1 grade_german_gr_w1MD grade_english_gr_w1 grade_english_w1MD privatteach_w1 parentssupport_w1 bfi_con_w1 bfi_neur_w1
	loc Vars_control 			bfi_open_w1 bfi_extra_w1 bfi_agree_w1 
	loc Vars_SES		 	    HigherSES 
		
	loc j=1
	foreach covariates in "" "" {
		loc i=1
		foreach IF_sample in "" {
			disp as err `i'
			loc name1 "full sample"
			disp as err "`name`i''"
			orth_out `dep_OutcomesSummary' 			using "${results}\\TableB2.xlsx" `IF_sample', by(Pairs_NoTwin_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') replace bdec(3) title(Main outcomes baseline (summary measures)) 
			orth_out `dep_Outcomes' 				using "${results}\\TableB2.xlsx" `IF_sample', by(Pairs_NoTwin_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Main outcomes baseline (components))
			noi cap orth_out `Vars_bal' 			using "${results}\\TableB2.xlsx" `IF_sample', by(Pairs_NoTwin_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Matching variables randomization baseline)
			noi cap orth_out `Vars_control' 		using "${results}\\TableB2.xlsx" `IF_sample', by(Pairs_NoTwin_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Further control variables baseline)
			noi cap orth_out `Vars_SES'		 		using "${results}\\TableB2.xlsx" `IF_sample', by(Pairs_NoTwin_w1) `covariates' overall comp vce(r) vcount test stars sheet(`name`i''_cov`j') vappend bdec(3) title(Higher-SES) notes(Sample: `name`i''; `covariates') count 
			loc ++i
		}
		loc ++j
	}
	beep

restore



******************************************************************************************************************
* Appendix D: Attrition Analysis
*******************************************************************************************************************

// Table D1: Sample Observations

tab treatment_w1 if $RandomSample 
tab attrition_overall_w2 treatment_w1 if $RandomSample
tab attrition_survey_w2 treatment_w1 if $RandomSample, col
tab attrition_survey_FB_w3 treatment_w1 if $RandomSample, col
tab attrition_survey_FB_w4 treatment_w1 if $RandomSample, col
tab attrition_admin_w2 treatment_w1  if $RandomSample, col
tab attrition_admin_final treatment_w1 if $RandomSample, col

tab treatment_w1 if $RandomSample & HigherSES == 0
tab attrition_overall_w2 treatment_w1  if $RandomSample & HigherSES == 0, col
tab attrition_survey_w2 treatment_w1 if $RandomSample & HigherSES == 0, col
tab attrition_survey_FB_w3 treatment_w1 if $RandomSample & HigherSES == 0, col
tab attrition_survey_FB_w4 treatment_w1 if $RandomSample & HigherSES == 0, col
tab attrition_admin_w2 treatment_w1  if $RandomSample & HigherSES == 0, col
tab attrition_admin_final treatment_w1 if $RandomSample & HigherSES == 0, col

tab treatment_w1 if $RandomSample & HigherSES == 1
tab attrition_overall_w2 treatment_w1  if $RandomSample & HigherSES == 1, col
tab attrition_survey_w2 treatment_w1 if $RandomSample & HigherSES == 1, col
tab attrition_survey_FB_w3 treatment_w1 if $RandomSample & HigherSES == 1, col
tab attrition_survey_FB_w4 treatment_w1 if $RandomSample & HigherSES == 1, col
tab attrition_admin_w2 treatment_w1  if $RandomSample & HigherSES == 1, col
tab attrition_admin_final treatment_w1 if $RandomSample & HigherSES == 1, col


// Table D2: Attrition Analysis One Year after Program Start

reg attrition_overall_w2 i.treatment_w1  $FE_pair if $RandomSample, r
reg attrition_overall_w2 c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_overall_w2 i.HigherSES##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_overall_w2 i.HigherSES##c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r

reg attrition_survey_w2 i.treatment_w1  $FE_pair if $RandomSample, r
reg attrition_survey_w2 c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_w2 i.HigherSES##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_w2 i.HigherSES##c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r

reg attrition_admin_w2 i.treatment_w1  $FE_pair if $RandomSample, r
reg attrition_admin_w2 c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_admin_w2 i.HigherSES##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_admin_w2 i.HigherSES##c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r


// Table D3: Attrition Analysis Two and Three Years after Program Start

reg attrition_survey_FB_w3 i.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_FB_w3 c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_FB_w3 i.HigherSES##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_FB_w3 i.HigherSES##c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r

reg attrition_survey_FB_w4 i.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_FB_w4 c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_FB_w4 i.HigherSES##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_survey_FB_w4 i.HigherSES##c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r

reg attrition_admin_final i.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_admin_final c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_admin_final i.HigherSES##ib0.treatment_w1 $FE_pair if $RandomSample, r
reg attrition_admin_final i.HigherSES##c.I_lmprospect_w1##ib0.treatment_w1 $FE_pair if $RandomSample, r

// Table D4: Effect of the Mentoring Program: Dropping Attrition Pairs

reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample, r
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & pair_attrition_overall_w2 == 0, r
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & pair_attrition_survey_w2 == 0, r
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & pair_attrition_admin_w2 == 0, r
reg I_lmprospect_w2 i.HigherSES##ib0.treatment_w1 I_lmprospect_w1M I_lmprospect_w1MD $control $FE_pair if $RandomSample & pair_attrition_survey_FB_w4 == 0, r



******************************************************************************************************************
* Appendix F: Mediation Analysis
*******************************************************************************************************************

* Rename vars
rename I_schooluseful_w2M schoolusefulM_w2
rename talkfuture_mentor_w2M talkmentM_w2
rename info1_mentor_w2M info1_mentorM_w2
rename I_schoolactI_w2 schoolactM_w2
rename I_impgrades_w2M impgradesM_w2

* Recode outcome variables
clonevar I_w2 = I_lmprospect_w2
clonevar I_w1M = I_lmprospect_w1M
clonevar I_w1MD = I_lmprospect_w1MD

clonevar M_w2 = math_post1
clonevar M_w1M = math_preM
clonevar M_w1MD = math_preMD

clonevar NC_w2 = I_patsocial_w2
clonevar NC_w1M = I_patsocial_w1M
clonevar NC_w1MD = I_patsocial_w1MD

clonevar LM_w2 = I_labororient_w2
clonevar LM_w1M = I_labororient_w1M
clonevar LM_w1MD = I_labororient_w1MD

* Set controls for mediation 
gl controladdlowSES 	info1_mentor_w1M info1_mentor_w1MD I_schooluseful_w1M I_schooluseful_w1MD
gl controladdhigherSES 	I_impgrades_w1


// Table F1: Effect of the Mentoring Program on Mediator Variables

* Low-SES sample
reg schoolusefulM_w2 i.treatment_w1 $control $controladdlowSES if $RandomSample & HigherSES == 0, r
reg talkmentM_w2 i.treatment_w1 $control $controladdlowSES if $RandomSample & HigherSES == 0, r
reg info1_mentorM_w2 i.treatment_w1 $control $controladdlowSES if $RandomSample & HigherSES == 0, r
* Higher-SES sample
reg schoolactM_w2 i.treatment_w1 $control $controladdhigherSES if $RandomSample & HigherSES == 1, r
reg impgradesM_w2 i.treatment_w1 $control $controladdhigherSES if $RandomSample & HigherSES == 1, r


// Table F2: Effect of the Mentoring Program on Labor-Market Prospects Conditional on Mediator Variables

* Low-SES
reg I_w2 i.treatment_w1 					I_w1M I_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r
reg I_w2 i.treatment_w1 schoolusefulM_w2 	I_w1M I_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r
reg I_w2 i.treatment_w1 talkmentM_w2		I_w1M I_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r
reg I_w2 i.treatment_w1 info1_mentorM_w2	I_w1M I_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r
reg I_w2 i.treatment_w1 schoolusefulM_w2 talkmentM_w2 info1_mentorM_w2	I_w1M I_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r

reg M_w2 i.treatment_w1 					M_w1M M_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r
reg M_w2 i.treatment_w1 schoolusefulM_w2 talkmentM_w2 info1_mentorM_w2	M_w1M M_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r

reg NC_w2 i.treatment_w1 					NC_w1M NC_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r
reg NC_w2 i.treatment_w1 schoolusefulM_w2 talkmentM_w2 NC_w1M NC_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r

reg LM_w2 i.treatment_w1 					LM_w1M LM_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r
reg LM_w2 i.treatment_w1 schoolusefulM_w2 info1_mentorM_w2 LM_w1M LM_w1MD $control  $controladdlowSES  if $RandomSample & HigherSES == 0, r

* Higher-SES
reg I_w2 i.treatment_w1 					I_w1M I_w1MD $control  $controladdhigherSES  if $RandomSample & HigherSES == 1, r
reg I_w2 i.treatment_w1 schoolactM_w2 impgradesM_w2	I_w1M I_w1MD $control  $controladdhigherSES  if $RandomSample & HigherSES == 1, r


// Mediation

/******************************************************************************************************************
* Step 1: Estimate treatment effect on potential mediators
(i) First, we estimate the treatment effect (eligibility for RYL!) on our mediator outcomes. We estimate the effect separately for low-SES and higher-SES. The estimates from these regressions are stored.
******************************************************************************************************************/
eststo clear

* Mediators low-SES
	
	loc mediatorlist "schoolusefulM talkmentM info1_mentorM"
	foreach item in `mediatorlist'{
	eststo ITT_`item'0: reg `item'_w2 i.treatment_w1 $control $controladdlowSES if $RandomSample & HigherSES == 0
	}		


* Mediators higher-SES
	
	loc mediatorlist "schoolactM impgradesM"
	foreach item in `mediatorlist'{
	eststo ITT_`item'1: reg `item'_w2 i.treatment_w1 $control $controladdhigherSES  if $RandomSample & HigherSES == 1
	}



/******************************************************************************************************************
* Step 2: Estimates of the contribution of mediators to the treatment effect of RYL!
(ii) Second, we estimate the correlation between our mediators and outcomes using the specification in equation (XX) of the paper. The estimates from these regressions are also stored.
*******************************************************************************************************************/

* Low-SES

* Overall index
foreach outcome in "I"{
global depvar 		`outcome'_w2
global lagdepvar 	`outcome'_w1M `outcome'_w1MD

	* Baseline update with additional controls
	eststo ITTcaddLS0`outcome': reg $depvar i.treatment_w1 $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0 

	* Each mediator separately
	loc mediatorlist "schoolusefulM talkmentM info1_mentorM"
	foreach item in `mediatorlist'{
	eststo ITTM_`item'0`outcome': reg $depvar i.treatment_w1 `item'_w2  $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0
	}	
				
	* For Outcome index = full model
	loc mediatorlist "schoolusefulM_w2 talkmentM_w2 info1_mentorM_w2"
	eststo ITTMLS_I_0`outcome': reg $depvar i.treatment_w1 `mediatorlist' $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0

}


* Math
foreach outcome in "M"{
global depvar 		`outcome'_w2
global lagdepvar 	`outcome'_w1M `outcome'_w1MD

	* Baseline update with additional controls
	eststo ITTcaddLS0`outcome': reg $depvar i.treatment_w1 $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0 
	
	* Each mediator separately
	loc mediatorlist "schoolusefulM talkmentM info1_mentorM"
	foreach item in `mediatorlist'{
	eststo ITTM_`item'0`outcome': reg $depvar i.treatment_w1 `item'_w2  $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0
	}	
	
	* For math = full model
	loc mediatorlist "schoolusefulM_w2 talkmentM_w2 info1_mentorM_w2"
	eststo ITTMLS_M_0`outcome': reg $depvar i.treatment_w1 `mediatorlist' $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0
		
}

* Non-cognitive skills and patience
foreach outcome in "NC"{
global depvar 		`outcome'_w2
global lagdepvar 	`outcome'_w1M `outcome'_w1MD
	
	* Baseline update with additional controls
	eststo ITTcaddLS0`outcome': reg $depvar i.treatment_w1 $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0 
	
	* Each mediator separately
	loc mediatorlist "schoolusefulM talkmentM"
	foreach item in `mediatorlist'{
	eststo ITTM_`item'0`outcome': reg $depvar i.treatment_w1 `item'_w2  $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0
	}	
	
	* For patience and social skills - omit info1_mentorM_w2 because it has negative contribution
	loc mediatorlist "schoolusefulM_w2 talkmentM_w2"
	eststo ITTMLS_NC_0`outcome': reg $depvar i.treatment_w1 `mediatorlist' $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0

}

* Labor-market orientation
foreach outcome in "LM"{
global depvar 		`outcome'_w2
global lagdepvar 	`outcome'_w1M `outcome'_w1MD

	loc mediatorlist "schoolusefulM talkmentM info1_mentorM"
	
	* Baseline update with additional controls
	eststo ITTcaddLS0`outcome': reg $depvar i.treatment_w1 $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0 
	
	* Each mediator separately
	loc mediatorlist "schoolusefulM info1_mentorM"
	foreach item in `mediatorlist'{
	eststo ITTM_`item'0`outcome': reg $depvar i.treatment_w1 `item'_w2  $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0
	}	
	
	* For labor-market orientation - omit talkmentM_w2 because it has negative contribution
	loc mediatorlist "schoolusefulM_w2 info1_mentorM_w2"
	eststo ITTMLS_LM_0`outcome': reg $depvar i.treatment_w1 `mediatorlist' $lagdepvar $control $controladdlowSES  if $RandomSample & HigherSES == 0

}


* Higher-SES - overall index

foreach outcome in "I"{
global depvar 		`outcome'_w2
global lagdepvar 	`outcome'_w1M `outcome'_w1MD

	* Baseline update with additional controls
	eststo ITTcaddHS1`outcome': reg $depvar i.treatment_w1 $lagdepvar $control $controladdhigherSES  if $RandomSample & HigherSES == 1
	
	* Each mediator separately
	loc mediatorlist "schoolactM impgradesM"
	foreach item in `mediatorlist'{
	eststo ITTM_`item'1`outcome': reg $depvar i.treatment_w1 `item'_w2  $lagdepvar $control $controladdhigherSES  if $RandomSample & HigherSES == 1
	}	
	
	* Full mediator model(s)
	loc mediatorlist "schoolactM_w2 impgradesM_w2"
	eststo ITTMHS_1`outcome': reg $depvar i.treatment_w1 `mediatorlist' $lagdepvar $control $controladdhigherSES  if $RandomSample & HigherSES == 1
	
}





/******************************************************************************************************************
* Step 3: Calculate share of treatment effect that is due to each of the intermediate outcomes using the estimates from steps i and ii using the "nlcom" command.
******************************************************************************************************************/

* Outcome index

loc mediatorlist "schoolusefulM talkmentM info1_mentorM"

foreach outcome in "I"{	
foreach ses in "0"{

* Combine estimates to adjust for common variance
#delimit ;
suest ITTcaddLS`ses'`outcome' 		ITTMLS_`outcome'_`ses'`outcome'
ITTM_schoolusefulM`ses'`outcome' 	ITTM_talkmentM`ses'`outcome' 	ITTM_info1_mentorM`ses'`outcome' 
ITT_schoolusefulM`ses'			ITT_talkmentM`ses' 				ITT_info1_mentorM`ses' 			
, robust
;
#delimit cr


/* Coefficient from the model with all mediators (and the treatment dummy) -- this is used as a weight of the importance of this mediator X 
			coefficient on treatment on mediator /
			overall treatment effect
			*/
		
foreach item in `mediatorlist'{
* fraction explained by mediator in single model
nlcom [ITTM_`item'`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mS`item'`ses'`outcome' = (0, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mS`item'`ses'`outcome'

* fraction of ITT explained by mediator in joint model
nlcom [ITTMLS_`outcome'_`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mJ`item'`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mJ`item'`ses'`outcome'
}
* fraction of ITT unexplained by mediators above (residual -- leaving out other "mediators", e.g., dummies for missing values)
#delimit ;
nlcom 1
- [ITTMLS_`outcome'_`ses'`outcome'_mean]schoolusefulM_w2 * [ITT_schoolusefulM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
- [ITTMLS_`outcome'_`ses'`outcome'_mean]talkmentM_w2 * [ITT_talkmentM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
- [ITTMLS_`outcome'_`ses'`outcome'_mean]info1_mentorM_w2 * [ITT_info1_mentorM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
;
#delimit cr
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid`ses'`outcome'
	
* fraction of ITT unexplained by mediators above (residual)
nlcom [ITTMLS_`outcome'_`ses'`outcome'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid_direct`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid_direct`ses'`outcome'
	
// Combine
* Joint contributions
matrix A`ses'`outcome' = (mallresid`ses'`outcome' \ mallresid_direct`ses'`outcome')
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mJ`item'`ses'`outcome')
}

* Single contributions
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mS`item'`ses'`outcome')
}
matrix list A`ses'`outcome'	
}
}



* Math

loc mediatorlist "schoolusefulM talkmentM info1_mentorM"

foreach outcome in "M"{	
foreach ses in "0"{

* Combine estimates to adjust for common variance
#delimit ;
suest ITTcaddLS`ses'`outcome' 		ITTMLS_`outcome'_`ses'`outcome'
ITTM_schoolusefulM`ses'`outcome' 	ITTM_talkmentM`ses'`outcome' 	ITTM_info1_mentorM`ses'`outcome' 
ITT_schoolusefulM`ses'			ITT_talkmentM`ses' 				ITT_info1_mentorM`ses' 			
, robust
;
#delimit cr


/* Coefficient from the model with all mediators (and the treatment dummy) -- this is used as a weight of the importance of this mediator X 
			coefficient on treatment on mediator /
			overall treatment effect
			*/
		
foreach item in `mediatorlist'{
* fraction explained by mediator in single model
nlcom [ITTM_`item'`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mS`item'`ses'`outcome' = (0, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mS`item'`ses'`outcome'

* fraction of ITT explained by mediator in joint model
nlcom [ITTMLS_`outcome'_`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mJ`item'`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mJ`item'`ses'`outcome'
}
* fraction of ITT unexplained by mediators above (residual -- leaving out other "mediators", e.g., dummies for missing values)
#delimit ;
nlcom 1
- [ITTMLS_`outcome'_`ses'`outcome'_mean]schoolusefulM_w2 * [ITT_schoolusefulM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
- [ITTMLS_`outcome'_`ses'`outcome'_mean]talkmentM_w2 * [ITT_talkmentM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
- [ITTMLS_`outcome'_`ses'`outcome'_mean]info1_mentorM_w2 * [ITT_info1_mentorM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
;
#delimit cr
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid`ses'`outcome'
	
* fraction of ITT unexplained by mediators above (residual)
nlcom [ITTMLS_`outcome'_`ses'`outcome'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid_direct`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid_direct`ses'`outcome'
	
// Combine
* Joint contributions
matrix A`ses'`outcome' = (mallresid`ses'`outcome' \ mallresid_direct`ses'`outcome')
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mJ`item'`ses'`outcome')
}

* Single contributions
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mS`item'`ses'`outcome')
}
matrix list A`ses'`outcome'	
}
}



* Patience and social skills

loc mediatorlist "schoolusefulM talkmentM"

foreach outcome in "NC"{	
foreach ses in "0"{

* Combine estimates to adjust for common variance
#delimit ;
suest ITTcaddLS`ses'`outcome' 		ITTMLS_NC_`ses'`outcome'
ITTM_schoolusefulM`ses'`outcome' 	ITTM_talkmentM`ses'`outcome' 	
ITT_schoolusefulM`ses'			ITT_talkmentM`ses' 				
, robust
;
#delimit cr

/* Coefficient from the model with all mediators (and the treatment dummy) -- this is used as a weight of the importance of this mediator X 
			coefficient on treatment on mediator /
			overall treatment effect
			*/
		
foreach item in `mediatorlist'{
* fraction explained by mediator in single model
nlcom [ITTM_`item'`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mS`item'`ses'`outcome' = (0, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mS`item'`ses'`outcome'

* fraction of ITT explained by mediator in joint model
nlcom [ITTMLS_NC_`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mJ`item'`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mJ`item'`ses'`outcome'
}
* fraction of ITT unexplained by mediators above (residual -- leaving out other "mediators", e.g., dummies for missing values)
#delimit ;
nlcom 1
- [ITTMLS_NC_`ses'`outcome'_mean]schoolusefulM_w2 * [ITT_schoolusefulM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
- [ITTMLS_NC_`ses'`outcome'_mean]talkmentM_w2 * [ITT_talkmentM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
;
#delimit cr
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid`ses'`outcome'
	
* fraction of ITT unexplained by mediators above (residual)
nlcom [ITTMLS_NC_`ses'`outcome'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid_direct`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid_direct`ses'`outcome'
	
// Combine
* Joint contributions
matrix A`ses'`outcome' = (mallresid`ses'`outcome' \ mallresid_direct`ses'`outcome')
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mJ`item'`ses'`outcome')
}

* Single contributions
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mS`item'`ses'`outcome')
}
matrix list A`ses'`outcome'	
}
}



* Labor-market orientation

loc mediatorlist "schoolusefulM info1_mentorM"

foreach outcome in "LM"{	
foreach ses in "0"{

* Combine estimates to adjust for common variance
#delimit ;
suest ITTcaddLS`ses'`outcome' 		ITTMLS_LM_`ses'`outcome'
ITTM_schoolusefulM`ses'`outcome'	ITTM_info1_mentorM`ses'`outcome' 	
ITT_schoolusefulM`ses' 			ITT_info1_mentorM`ses' 			
, robust
;
#delimit cr

/* Coefficient from the model with all mediators (and the treatment dummy) -- this is used as a weight of the importance of this mediator X 
			coefficient on treatment on mediator /
			overall treatment effect
			*/
		
foreach item in `mediatorlist'{
* fraction explained by mediator in single model
nlcom [ITTM_`item'`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mS`item'`ses'`outcome' = (0, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mS`item'`ses'`outcome'

* fraction of ITT explained by mediator in joint model
nlcom [ITTMLS_LM_`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mJ`item'`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mJ`item'`ses'`outcome'
}
* fraction of ITT unexplained by mediators above (residual -- leaving out other "mediators", e.g., dummies for missing values)
#delimit ;
nlcom 1
- [ITTMLS_LM_`ses'`outcome'_mean]schoolusefulM_w2 * [ITT_schoolusefulM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
- [ITTMLS_LM_`ses'`outcome'_mean]info1_mentorM_w2 * [ITT_info1_mentorM`ses'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
;
#delimit cr
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid`ses'`outcome'
	
* fraction of ITT unexplained by mediators above (residual)
nlcom [ITTMLS_LM_`ses'`outcome'_mean]1.treatment_w1 / [ITTcaddLS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	
	matrix mallresid_direct`ses'`outcome' = (1, `ses', share, se_share, share/se_share, 2*normal(-abs(share/se_share)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid_direct`ses'`outcome'
	
// Combine
* Joint contributions
matrix A`ses'`outcome' = (mallresid`ses'`outcome' \ mallresid_direct`ses'`outcome')
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mJ`item'`ses'`outcome')
}

* Single contributions
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mS`item'`ses'`outcome')
}
matrix list A`ses'`outcome'	
}
}

* Combine all estimates
foreach ses in "0"{
matrix ALLOmitt`ses' = (A`ses'I \ A`ses'M \ A`ses'NC \ A`ses'LM)
}
matrix list ALLOmitt0


preserve

svmat ALLOmitt0, names(w)
keep w1-w8
drop if w1 == .
format w3-w8 %9.3f

gen n = _n

rename w1 JOINT
la def JOINT 1 "[1] Contribution from joint model" 0 "[0] Contribution from single model", replace
la val JOINT JOINT

rename w2 HigherSES
la def HigherSES 1 "[1] Higher-SES" 0 "[0] Low-SES", replace
la val HigherSES HigherSES

gen outcome = .
replace outcome = 0 if n >= 1 & n <= 8 
replace outcome = 1 if n >= 9 & n <= 16 
replace outcome = 2 if n >= 17 & n <= 22
replace outcome = 3 if n >= 23 & n <= 28

la def outcome 0 "[0] Labor-market prospects" 1 "[1] Math achievement in school" 2 "[2] Patience and social skills" 3 "[3] Labor-market orientation", replace
la val outcome outcome

rename w3 sharepart
rename w4 sharepart_se
rename w5 zstatpart
rename w6 pvaluepart
rename w7 CI95lowpart
rename w8 CI95toppart

gen variable = .
replace variable = 0 if n == 1 | n == 9 | n == 17 | n == 23 
replace variable = 99 if n == 2 | n == 10 | n == 18 | n == 24

replace variable = 1 if inlist(n, 3, 6, 11, 14, 19, 21, 25, 27)
replace variable = 2 if inlist(n, 4, 7, 12, 15, 20, 22)
replace variable = 3 if variable == .

la def variable 0 "[0] Residual" 99 "[99] Residual-direct" 1 "[1] Perceived usefulness of the school for future jobs" 2 "[2] Talk about the future with mentor" 3 "[3] Mentor important for receiving job information", replace
la val variable variable

drop n
order HigherSES outcome variable JOINT

save "${figure}\mediation_lowSES.dta", replace
restore



// Model 2: Higher-SES Mediation

loc mediatorlist "schoolactM impgradesM"

foreach outcome in "I"{	

foreach ses in "1"{
* Combine estimates to adjust for common variance
#delimit ;
suest ITTcaddHS`ses'`outcome' ITTMHS_`ses'`outcome'
ITTM_schoolactM`ses'`outcome' 	ITTM_impgradesM`ses'`outcome'
ITT_schoolactM`ses' 			ITT_impgradesM`ses'
, robust
;
#delimit cr

/* Coefficient from the model with all mediators (and the treatment dummy) -- this is used as a weight of the importance of this mediator X 
			coefficient on treatment on mediator /
			overall treatment effect
			*/
		
foreach item in `mediatorlist'{
* fraction explained by mediator in single model
nlcom [ITTM_`item'`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddHS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	scalar zstat = share/se_share
	
	matrix mS`item'`ses'`outcome' = (0, `ses', share, se_share, zstat, 2*normal(-abs(zstat)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mS`item'`ses'`outcome'

* fraction of ITT explained by mediator in joint model
nlcom [ITTMHS_`ses'`outcome'_mean]`item'_w2 * [ITT_`item'`ses'_mean]1.treatment_w1 / [ITTcaddHS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	scalar zstat = share/se_share
	
	matrix mJ`item'`ses'`outcome' = (1, `ses', share, se_share, zstat, 2*normal(-abs(zstat)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mJ`item'`ses'`outcome'
}
* fraction of ITT unexplained by mediators above (residual -- leaving out other "mediators", e.g., dummies for missing values)
#delimit ;
nlcom 1
- [ITTMHS_`ses'`outcome'_mean]schoolactM_w2 * [ITT_schoolactM`ses'_mean]1.treatment_w1 / [ITTcaddHS`ses'`outcome'_mean]1.treatment_w1
- [ITTMHS_`ses'`outcome'_mean]impgradesM_w2 * [ITT_impgradesM`ses'_mean]1.treatment_w1 / [ITTcaddHS`ses'`outcome'_mean]1.treatment_w1
;
#delimit cr
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	scalar zstat = share/se_share
	
	matrix mallresid`ses'`outcome' = (1, `ses', share, se_share, zstat, 2*normal(-abs(zstat)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid`ses'`outcome'
	
* fraction of ITT unexplained by mediators above (residual)
nlcom [ITTMHS_`ses'`outcome'_mean]1.treatment_w1 / [ITTcaddHS`ses'`outcome'_mean]1.treatment_w1
	scalar share = (r(b)[1,1])
	scalar se_share = (sqrt(r(V)[1,1]))
	scalar zstat = share/se_share
	
	matrix mallresid_direct`ses'`outcome' = (1, `ses', share, se_share, zstat, 2*normal(-abs(zstat)), share - invnormal(0.975)* se_share, share + invnormal(0.975)* se_share)
	matrix list mallresid_direct`ses'`outcome'
	
// Combine
* Joint contributions
matrix A`ses'`outcome' = (mallresid`ses'`outcome' \ mallresid_direct`ses'`outcome')
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mJ`item'`ses'`outcome')
}

* Single contributions
foreach item in `mediatorlist'{
matrix A`ses'`outcome' = (A`ses'`outcome' \ mS`item'`ses'`outcome')
}
matrix list A`ses'`outcome'	
}
}


svmat A1I, names(w)
keep w1-w8
drop if w1 == .
format w3-w8 %9.3f

gen n = _n

rename w1 JOINT
la def JOINT 1 "[1] Contribution from joint model" 0 "[0] Contribution from single model"
la val JOINT JOINT

rename w2 HigherSES
la def HigherSES 1 "[1] Higher-SES" 0 "[0] Low-SES"
la val HigherSES HigherSES

gen outcome = 0
la def outcome 0 "[0] Labor-market prospects" 1 "[1] Math achievement in school" 2 "[2] Patience and social skills" 3 "[3] Labor-market orientation"
la val outcome outcome

rename w3 share
rename w4 share_se
rename w5 zstat
rename w6 pvalue
rename w7 CI95low
rename w8 CI95top

gen variable = .
replace variable = 0 if inlist(n, 1)
replace variable = 99 if inlist(n, 2)
replace variable = 4 if inlist(n, 3, 5)
replace variable = 5 if inlist(n, 4, 6)

la def variable 0 "[0] Residual" 99 "[99] Residual-direct" 1 "[1] Perceived usefulness of the school for future jobs" 2 "[2] Talk about the future with mentor" 3 "[3] Mentor important for receiving job information"  ///
4 "[4] Activities in school" 5 "[5] Importance of good grades" , replace
la val variable variable

drop n
order HigherSES outcome variable JOINT




* Figure 4: Share of Treatment Effect for Low-SES Adolescents Attributed to Mediators

use "${figure}\mediation_lowSES.dta", clear

keep if HigherSES == 0 & variable != 99
keep outcome variable JOINT sharepart
keep if outcome == 0 | outcome != 0 & JOINT == 1

set obs 19
gen n = _n
replace outcome = 2 if n == 18
replace variable = 3 if n == 18
replace JOINT = 1 if n == 18
replace sharepart = 0 if n == 18

replace outcome = 3 if n == 19
replace variable = 2 if n == 19
replace JOINT = 1 if n == 19
replace sharepart = 0 if n == 19

gsort outcome -JOINT variable

set obs 23
drop n
gen n = _n
replace outcome = 0 if n == 20
replace variable = 50 if n == 20
replace JOINT = 1 if n == 20

replace outcome = 1 if n == 21
replace variable = 50 if n == 21
replace JOINT = 1 if n == 21

replace outcome = 2 if n == 22
replace variable = 50 if n == 22
replace JOINT = 1 if n == 22

replace outcome = 3 if n == 23
replace variable = 50 if n == 23
replace JOINT = 1 if n == 23

forval cc = 0/3{
gen share`cc' = sharepart if JOINT == 1 & variable == `cc'
sort outcome share`cc' 
bysort outcome: replace share`cc' = share`cc'[1]
gen label`cc' = string(share`cc'*100, "%2.0f") + "%"
}

gen marker1 = share1 / 2
	replace marker1 = . if share1 == 0
gen marker2 = share1 + share2 / 2
	replace marker2 = . if share2 == 0
gen marker3 = share1 + share2 + share3 / 2
	replace marker3 = . if share3 == 0
gen marker_resid = share1 + share2 + share3 + share0/2



forval cc = 1/3{
gen sharesingle`cc' = sharepart if JOINT == 0 & variable == `cc'
sort outcome sharesingle`cc' 
bysort outcome: replace sharesingle`cc' = sharesingle`cc'[1]
gen sharesingle`cc'_resid = 1 - sharesingle`cc'
gen labelsingle`cc' = string(sharesingle`cc'*100, "%2.0f") + "%"
gen labelsingle`cc'_resid = string(sharesingle`cc'_resid*100, "%2.0f") + "%"
gen markersingle`cc' = sharesingle`cc' / 2
gen markersingle`cc'_resid = sharesingle`cc' + sharesingle`cc'_resid / 2
}

sort outcome JOINT variable

gen share_acc1 = share1
gen share_acc2 = share1 + share2
gen share_acc3 = share1 + share2 + share3
gen share_total = 1 

format share* %9.3f

gen order = 3 if variable == 1 	// Perceive school as useful for job
replace order = 2 if variable == 2	// Talk with mentor about future
replace order = 1 if variable == 3	// Mentor important for job choice 
replace order = 0 if variable == 50 & outcome == 0 	// LM prospects
replace order = -1 if variable == 50 & outcome == 1	// Math achievement
replace order = -2 if variable == 50 & outcome == 2	// Patience and social skills
replace order = -3 if variable == 50 & outcome == 3	// LM orientation


* Define colors
colorpalette HCL blues, n(9) locals
local colorjobinfo `r(p9)'
local colortalkfuture `r(p5)'
local colorschooluseful `r(p1)'


* Overall index
#delimit ;
graph twoway
(bar share_total order if JOINT == 0 & variable == 1, horizontal color(white) lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar sharesingle1 order if JOINT == 0 & variable == 1, horizontal color("`colorschooluseful'") lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar share_total order if JOINT == 0 & variable == 2, horizontal color(white) lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar sharesingle2 order if JOINT == 0 & variable == 2, horizontal color("`colortalkfuture'") lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar share_total order if JOINT == 0 & variable == 3, horizontal color(white) lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar sharesingle3 order if JOINT == 0 & variable == 3, horizontal color("`colorjobinfo'") lcolor(gs12) lwidth(vthin) barwidth(.7))

(bar share_total order if JOINT == 1 & variable == 50 & order == 0, horizontal color(white) lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar share_acc3 order if JOINT == 1 & variable == 50 & order == 0, horizontal color("`colorjobinfo'") lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar share_acc2 order if JOINT == 1 & variable == 50 & order == 0, horizontal color("`colortalkfuture'") lcolor(gs12) lwidth(vthin) barwidth(.7))
(bar share_acc1 order if JOINT == 1 & variable == 50 & order == 0, horizontal color("`colorschooluseful'") lcolor(gs12) lwidth(vthin) barwidth(.7))

(scatter order markersingle1 if JOINT == 0 & variable == 1, msymbol(i) mlabpos(0) mlabel(labelsingle1) mlabcolor(black) mlabsize(small))
(scatter order markersingle1_resid if JOINT == 0 & variable == 1, msymbol(i) mlabpos(0) mlabel(labelsingle1_resid) mlabcolor(black) mlabsize(small))
(scatter order markersingle2 if JOINT == 0 & variable == 2, msymbol(i) mlabpos(0) mlabel(labelsingle2) mlabcolor(black) mlabsize(small))
(scatter order markersingle2_resid if JOINT == 0 & variable == 2, msymbol(i) mlabpos(0) mlabel(labelsingle2_resid) mlabcolor(black) mlabsize(small))
(scatter order markersingle3 if JOINT == 0 & variable == 3, msymbol(i) mlabpos(0) mlabel(labelsingle3) mlabcolor(black) mlabsize(small))
(scatter order markersingle3_resid if JOINT == 0 & variable == 3, msymbol(i) mlabpos(0) mlabel(labelsingle3_resid) mlabcolor(black) mlabsize(small))

(scatter order marker1 if JOINT == 1 & variable == 50 & order == 0, msymbol(i) mlabpos(0) mlabel(label1) mlabcolor(black) mlabsize(small))
(scatter order marker2 if JOINT == 1 & variable == 50 & order == 0, msymbol(i) mlabpos(0) mlabel(label2) mlabcolor(black) mlabsize(small))
(scatter order marker3 if JOINT == 1 & variable == 50 & order == 0, msymbol(i) mlabpos(0) mlabel(label3) mlabcolor(black) mlabsize(small))
(scatter order marker_resid if JOINT == 1 & variable == 50 & order == 0, msymbol(i) mlabpos(0) mlabel(label0) mlabcolor(black) mlabsize(small)),

legend(off)

xline(0, lcolor(black))
graphregion(color(white) margin(5 5 5 5))
xscale(range(0 1) titlegap(3)) xlabel(0 "0%" 0.2 "20%" 0.4 "40%" 0.6 "60%" 0.8 "80%" 1 "100%", nogrid) 
xtitle("")
ytitle("")
title("Panel A: Index of labor-market prospects", pos(11) color(black) size(medlarge))
yscale(lstyle(none))
ylabel(0 "All channels combined" 3 `" "Channel 1: Perceive           " "school as useful for job" "' 2 `" "Channel 2: Talk with" "mentor about future" "' 1 `" "Channel 3: Mentor" "important for job choice" "' , labsize(medsmall) tlcolor(white) nogrid angle(0))
name(lmprospects, replace)
;
#delimit cr


* Components
#delimit ;
graph twoway
(bar share_total order if JOINT == 1 & variable == 50 & order == -1, horizontal color(white) lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc3 order if JOINT == 1 & variable == 50 & order == -1, horizontal color("`colorjobinfo'") lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc2 order if JOINT == 1 & variable == 50 & order == -1, horizontal color("`colortalkfuture'") lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc1 order if JOINT == 1 & variable == 50 & order == -1, horizontal color("`colorschooluseful'") lcolor(gs12) lwidth(vthin) barwidth(.55))

(bar share_total order if JOINT == 1 & variable == 50 & order == -2, horizontal color(white) lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc3 order if JOINT == 1 & variable == 50 & order == -2, horizontal color("`colorjobinfo'") lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc2 order if JOINT == 1 & variable == 50 & order == -2, horizontal color("`colortalkfuture'") lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc1 order if JOINT == 1 & variable == 50 & order == -2, horizontal color("`colorschooluseful'") lcolor(gs12) lwidth(vthin) barwidth(.55))

(bar share_total order if JOINT == 1 & variable == 50 & order == -3, horizontal color(white) lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc3 order if JOINT == 1 & variable == 50 & order == -3, horizontal color("`colorjobinfo'") lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc2 order if JOINT == 1 & variable == 50 & order == -3, horizontal color("`colortalkfuture'") lcolor(gs12) lwidth(vthin) barwidth(.55))
(bar share_acc1 order if JOINT == 1 & variable == 50 & order == -3, horizontal color("`colorschooluseful'") lcolor(gs12) lwidth(vthin) barwidth(.55))

(scatter order marker1 if JOINT == 1 & variable == 50 & order != 0, msymbol(i) mlabpos(0) mlabel(label1) mlabcolor(black) mlabsize(small))
(scatter order marker2 if JOINT == 1 & variable == 50 & order != 0, msymbol(i) mlabpos(0) mlabel(label2) mlabcolor(black) mlabsize(small))
(scatter order marker3 if JOINT == 1 & variable == 50 & order != 0, msymbol(i) mlabpos(0) mlabel(label3) mlabcolor(black) mlabsize(small))
(scatter order marker_resid if JOINT == 1 & variable == 50 & order != 0, msymbol(i) mlabpos(0) mlabel(label0) mlabcolor(black) mlabsize(small)),

legend(order(4 3 2 1) row(2) size(small)
label(1 "Other factors")
label(2 "Mentor important for job choice")
label(3 "Talk with mentor about future")
label(4 "Perceive school as useful for job"))

xline(0, lcolor(black))
graphregion(color(white) margin(5 5 0 0)) 
xscale(range(0 1) titlegap(3)) xlabel(0 "0%" 0.2 "20%" 0.4 "40%" 0.6 "60%" 0.8 "80%" 1 "100%", nogrid) 
xtitle("")
ytitle("")
title("Panel B: Components", pos(11) color(black) size(medlarge))
yscale(lstyle(none))
ylabel(-1 "Math achievement in school" -2 "Patience and social skills" -3 "Labor-market orientation", labsize(medsmall) tlcolor(white) nogrid angle(0))
name(components, replace)
;
#delimit cr
graph combine lmprospects components, row(2) graphregion(color(white))











